home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFINTO.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  5KB  |  160 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to read stdin, and save it into the specified file iff the result  *
  7. * and inspired by the rle utah tool kit I decided to implement and add it.   *
  8. * -q : quite printing mode.                             *
  9. * -s minsize : the minimum file size to keep it.                 *
  10. * -h : on line help.                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 7 Jul 89 - Version 1.0 by Gershon Elber.                     *
  14. * 22 Dec 89 - Fix problem with tmpnam (Version 1.1).                         *
  15. *****************************************************************************/
  16.  
  17. #ifdef __MSDOS__
  18. #include <io.h>
  19. #include <stdlib.h>
  20. #include <alloc.h>
  21. #endif /* __MSDOS__ */
  22.  
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include "gif_lib.h"
  28. #include "getarg.h"
  29.  
  30. #define PROGRAM_NAME    "GifInto"
  31.  
  32. #define DEFAULT_MIN_FILE_SIZE    14     /* More than GIF stamp + screen desc. */
  33. #define    DEFAULT_OUT_NAME    "GifInto.Gif"
  34. #define DEFAULT_TMP_NAME    "TempInto.$$$"
  35.  
  36. #ifdef __MSDOS__
  37. extern unsigned int
  38.     _stklen = 16384;                 /* Increase default stack size. */
  39. #endif /* __MSDOS__ */
  40.  
  41. #ifdef SYSV
  42. static char *VersionStr =
  43.         "Gif library module,\t\tGershon Elber\n\
  44.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  45. static char
  46.     *CtrlStr = "GifInto q%- s%-MinFileSize!d h%- GifFile!*s";
  47. #else
  48. static char
  49.     *VersionStr =
  50.     PROGRAM_NAME
  51.     GIF_LIB_VERSION
  52.     "    Gershon Elber,    "
  53.     __DATE__ ",   " __TIME__ "\n"
  54.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  55. static char
  56.     *CtrlStr =
  57.     PROGRAM_NAME
  58.     " q%- s%-MinFileSize!d h%- GifFile!*s";
  59. #endif /* SYSV */
  60.  
  61. static int
  62.     MinFileSize = DEFAULT_MIN_FILE_SIZE;
  63.  
  64. /******************************************************************************
  65. * The is simply: read until EOF, then close the output, test its length, and  *
  66. * if non zero then rename it.                              *
  67. ******************************************************************************/
  68. void main(int argc, char **argv)
  69. {
  70.     int    Error, NumFiles,
  71.     MinSizeFlag = FALSE, HelpFlag = FALSE;
  72.     char **FileName = NULL,
  73.         TmpName[80], FoutTmpName[80], FullPath[80], DefaultName[80], s[80], *p;
  74.     FILE *Fin, *Fout;
  75.  
  76.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  77.         &MinSizeFlag, &MinFileSize, &HelpFlag,
  78.         &NumFiles, &FileName)) != FALSE ||
  79.         (NumFiles > 1 && !HelpFlag)) {
  80.     if (Error)
  81.         GAPrintErrMsg(Error);
  82.     else if (NumFiles != 1)
  83.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  84.     GAPrintHowTo(CtrlStr);
  85.     exit(1);
  86.     }
  87.  
  88.     if (HelpFlag) {
  89.     fprintf(stderr, VersionStr);
  90.     GAPrintHowTo(CtrlStr);
  91.     exit(0);
  92.     }
  93.  
  94.     /* Open the stdin in binary mode and increase its buffer size: */
  95. #ifdef __MSDOS__
  96.     setmode(0, O_BINARY);          /* Make sure it is in binary mode. */
  97.     if ((Fin = fdopen(0, "rb")) == NULL ||       /* Make it into a stream: */
  98.         setvbuf(Fin, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE))/* Incr. stream buf.*/
  99. #else
  100.     if ((Fin = fdopen(0, "r")) == NULL)        /* Make it into a stream: */
  101. #endif /* __MSDOS__ */
  102.     GIF_EXIT("Failed to open input.");
  103.  
  104.     /* Isolate the directory where our destination is, and set tmp file name */
  105.     /* in the very same directory.                         */
  106.     strcpy(FullPath, *FileName);
  107.     if ((p = strrchr(FullPath, '/')) != NULL ||
  108.     (p = strrchr(FullPath, '\\')) != NULL)
  109.     p[1] = 0;
  110.     else if ((p = strrchr(FullPath, ':')) != NULL)
  111.     p[1] = 0;
  112.     else
  113.     FullPath[0] = 0;          /* No directory or disk specified. */
  114.  
  115.     strcpy(FoutTmpName, FullPath);   /* Generate destination temporary name. */
  116.     /* Make sure the temporary is made in the current directory: */
  117.     p = tmpnam(TmpName);
  118.     if (strrchr(p, '/')) p = strrchr(p, '/') + 1;
  119.     if (strrchr(p, '\\')) p = strrchr(p, '\\') + 1;
  120.     if (strlen(p) == 0) p = DEFAULT_TMP_NAME;
  121.     strcat(FoutTmpName, p);
  122.  
  123. #ifdef __MSDOS__
  124.     if ((Fout = fopen(FoutTmpName, "wb")) == NULL ||
  125.     setvbuf(Fout, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE))/*Incr. stream buf.*/
  126. #else
  127.     if ((Fout = fopen(FoutTmpName, "w")) == NULL)
  128. #endif /* __MSDOS__ */
  129.     GIF_EXIT("Failed to open output.");
  130.  
  131.     while (!feof(Fin)) {
  132.     if (putc(getc(Fin), Fout) == EOF)
  133.         GIF_EXIT("Failed to write output.");
  134.     }
  135.  
  136.     fclose(Fin);
  137.     if (ftell(Fout) >= (long) MinFileSize) {
  138.     fclose(Fout);
  139.     unlink(*FileName);
  140.     if (rename(FoutTmpName, *FileName) != 0) {
  141.         strcpy(DefaultName, FullPath);
  142.         strcat(DefaultName, DEFAULT_OUT_NAME);
  143.         if (rename(FoutTmpName, DefaultName) == 0) {
  144.         sprintf(s, "Failed to rename out file - left as %s.",
  145.                                 DefaultName);
  146.         GIF_MESSAGE(s);
  147.         }
  148.         else {
  149.         unlink(FoutTmpName);
  150.         GIF_MESSAGE("Failed to rename out file - deleted.");
  151.         }
  152.     }
  153.     }
  154.     else {
  155.     fclose(Fout);
  156.     unlink(FoutTmpName);
  157.     GIF_MESSAGE("File too small - not renamed.");
  158.     }
  159. }
  160.